
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using WebApp.Navigations;
using WebApp.Navigations.Dtos;

using WebApp.Navigations.DomainService;
using WebApp.Authorization;

namespace WebApp.Navigations
{
	/// <summary>
	/// 应用层服务的接口实现方法
	///</summary>
	[AbpAuthorize]
	public class MenuElementAppService : WebAppAppServiceBase, IMenuElementAppService
	{
		private readonly IRepository<MenuElement, Guid> _menuElementRepository;



		private readonly IMenuElementManager _menuElementManager;
		private readonly IUiMenuManager _uiMenuManagerManager;
		/// <summary>
		/// 构造函数
		///</summary>
		public MenuElementAppService(
		IRepository<MenuElement, Guid> menuElementRepository
			  , IMenuElementManager menuElementManager
			  , IUiMenuManager uiMenuManagerManager

			 )
		{
			_menuElementRepository = menuElementRepository;
			_menuElementManager = menuElementManager;
			_uiMenuManagerManager = uiMenuManagerManager;


		}


		/// <summary>
		/// 获取的分页列表信息
		///      </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(MenuElementPermissions.Query)]
		public async Task<PagedResultDto<MenuElementListDto>> GetPaged(GetMenuElementsInput input)
		{

			var query = _menuElementRepository.GetAll()

						  //模糊搜索Name
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Name.Contains(input.FilterText))
						  //模糊搜索DisplayName
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.DisplayName.Contains(input.FilterText))
						  //模糊搜索CssStyle
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CssStyle.Contains(input.FilterText))
						  //模糊搜索PermissionKey
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PermissionKey.Contains(input.FilterText))
						  //模糊搜索Remark
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Remark.Contains(input.FilterText))
						  //模糊搜索Icon
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Icon.Contains(input.FilterText))
			; if (input.UiMenuId != null && input.UiMenuId != Guid.Empty)
			{
				query = query.Where(u => u.UiMenuId == input.UiMenuId);
			}
			// TODO:根据传入的参数添加过滤条件

			var count = await query.CountAsync();

			var menuElementList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			var menuElementListDtos = ObjectMapper.Map<List<MenuElementListDto>>(menuElementList);

			return new PagedResultDto<MenuElementListDto>(count, menuElementListDtos);
		}


		/// <summary>
		/// 通过指定id获取MenuElementListDto信息
		/// </summary>
		[AbpAuthorize(MenuElementPermissions.Query)]
		public async Task<MenuElementListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _menuElementRepository.GetAsync(input.Id);

			var dto = ObjectMapper.Map<MenuElementListDto>(entity);
			return dto;
		}

		/// <summary>
		/// 获取编辑 
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(MenuElementPermissions.Create, MenuElementPermissions.Edit)]
		public async Task<GetMenuElementForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetMenuElementForEditOutput();
			MenuElementEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _menuElementRepository.GetAsync(input.Id.Value);
				editDto = ObjectMapper.Map<MenuElementEditDto>(entity);
			}
			else
			{
				editDto = new MenuElementEditDto();
			}



			output.MenuElement = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(MenuElementPermissions.Create, MenuElementPermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateMenuElementInput input)
		{

			if (input.MenuElement.Id.HasValue)
			{
				await Update(input.MenuElement);
			}
			else
			{
				await Create(input.MenuElement);
			}
		}


		/// <summary>
		/// 新增
		/// </summary>
		[AbpAuthorize(MenuElementPermissions.Create)]
		public async Task<MenuElementEditDto> Create(MenuElementEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

			var entity = ObjectMapper.Map<MenuElement>(input);
			//调用领域服务
			entity = await _menuElementManager.CreateAsync(entity);

			var dto = ObjectMapper.Map<MenuElementEditDto>(entity);
			return dto;
		}

		/// <summary>
		/// 编辑
		/// </summary>
		[AbpAuthorize(MenuElementPermissions.Edit)]
		public async Task Update(MenuElementEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _menuElementRepository.GetAsync(input.Id.Value);
			//  input.MapTo(entity);
			//将input属性的值赋值到entity中
			ObjectMapper.Map(input, entity);
			await _menuElementManager.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(MenuElementPermissions.Delete)]
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _menuElementManager.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除MenuElement的方法
		/// </summary>
		[AbpAuthorize(MenuElementPermissions.BatchDelete)]
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _menuElementManager.BatchDelete(input);
		}




		//// custom codes

		/// <summary>
		/// 获取所有列表的按钮
		/// </summary>
		[AbpAuthorize(MenuElementPermissions.Query, UiMenuPermissions.Node, PermissionNames.Pages_Roles)]
		public async Task<List<UiMenuBtuPidArrDto>> getAllUiMenuEle()
		{
			List<UiMenuBtuPidArrDto> uiPid = new List<UiMenuBtuPidArrDto>(); 
			var AllEle= await _menuElementManager.QueryMenuElementsAsNoTracking().Where(u=>u.IsEnable).OrderBy(u=>u.Order).ToListAsync();
			var UiMenuS = AllEle.Select(u => u.UiMenuId).Distinct();
			foreach (var item in UiMenuS)
			{
				UiMenuBtuPidArrDto uiele = new UiMenuBtuPidArrDto();
				uiele.uiMenuId = item;
				uiele.uiMenuArr= await _uiMenuManagerManager.GetPUiMenuNmae(item);
				 uiele.uiMenuArr.Reverse();
				uiele.MenuElements = AllEle.Where(u => u.UiMenuId == item&& u.IsEnable).Select(u => ObjectMapper.Map<MenuElementEditDto>(u)).ToList();
				uiPid.Add(uiele);
			}

			return uiPid;

		}
		//// custom codes end
	}
}


